﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using D3;
using KCBF.Modules.AI.TreeSharp;
using NYTBD;
using NYTBD.Modules;
using Action = KCBF.Modules.AI.TreeSharp.Action;

namespace KCBF.Modules.AI.QuestSteps.Substeps
{
    class InteractWithNpc : Decorator
    {

        private bool _isFinnished = false;
        private string _target = "";

        public InteractWithNpc(string target) : base(null, null)
        {
            _target = target;
            Runner = delegate(object context)
                         {
                             if (_isFinnished)
                                return false;
                             Unit u = _Game.GetUnit(target);
                             if (u == null)
                                return false;
                             if (Squanto.GetDistance(u.X, u.Y) > 3) return false;
                             return true;
                         };

            PrioritySelector seq = new PrioritySelector();

            seq.AddChild(new Action(delegate
                                         {
                                             if (_Game.GetUnit(target) == null) return RunStatus.Failure;
                                             Plato.Interact(target);
                                             _isFinnished = true;
                                             return RunStatus.Failure;
                                         }));

            seq.AddChild(new WaitContinue(200, rem => false, new ActionAlwaysSucceed()));

            Children[0] = seq;
        }

        public override string ToString()
        {
            return "Interact With: " + _target;
        }

        public override IEnumerable<RunStatus> Execute(object context)
        {
            Game.Print(ToString());
            return base.Execute(context);
        }
    }
}
